package com.ly.lypopupwindowdemo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.fragment.Fragment2;
import com.ly.lypopupwindowdemo.fragment.Fragment3;

public class FragmentStudyActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_study);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(this);
        replaceFragment(new Fragment2());

    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);

        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new Fragment3());
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        /**
         * getMenuInflater()方法可以得到MenuInflater对象
         * 在调用inflate方法给当前活动创建菜单
         * 其中第一个参数指定资源文件
         * 第二个参数指定我们的菜单项将添加到哪一个Memu对象中
         *
         * 返回true表示允许创建的菜单显示出来，返回false创建的菜单将无法显示
         */
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.main1:
                Toast.makeText(this, "Main1", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.main2:
                Toast.makeText(this, "Main2", Toast.LENGTH_SHORT).show();
                return true;
            default:
                //如果希望Fragment自己处理MenuItem点击事件，一定不要忘了调用super.xxx
                return super.onOptionsItemSelected(item);
                //return true;
        }
    }
}
